package cn.book.system.controller;
import cn.book.basic.utils.AjaxResult;
import cn.book.basic.utils.PageList;
import cn.book.system.Service.IDicDetailService;
import cn.book.system.domain.DicDetail;
import cn.book.system.query.DicDetailQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 部门controller类
 * @Author: Mr.She
 * @Version: 1.0
 * @Date: 2022/6/23 21:42
 */
@RestController
@RequestMapping("/detail")
public class DicDetailController {

    @Autowired
    private IDicDetailService dicDetailService;

    /**
    * @Title: loadById
    * @Description: 根据Id查询
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.book.org.domain.DicDetail
    */
    @GetMapping("/{id}")
    public DicDetail loadById(@PathVariable("id") Long id) {
        return dicDetailService.loadById(id);
    }

    /**
     * @Title: loadAll
     * @Description: 查询所有
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/24 9:35
     * @Parameters: []
     * @Return java.util.List<cn.itsource.org.domain.DicDetail>
     */
    @GetMapping// 请求地址：http://localhost:8080/dicDetail GET请求
    public List<DicDetail> loadAll(){
        return dicDetailService.loadAll();
    }

    /**
    * @Title: queryData
    * @Description: 分页查询
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 10:12
    * @Parameters: [dicDetailQuery]
    * @Return cn.book.basic.utils.PageList<cn.book.org.domain.DicDetail>
     * @return cn.book.basic.utils.AjaxResult
    */
    @PostMapping // 可以直接访问
    public PageList<DicDetail> queryData(@RequestBody DicDetailQuery dicDetailQuery) {

            PageList<DicDetail> data = dicDetailService.queryData(dicDetailQuery);
            return data;
    }

    /**
    * @Title: remove
    * @Description: 根据Id删除
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 9:59
    * @Parameters: [id]
    * @Return cn.book.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            dicDetailService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    /**
    * @Title: saveOrUpdate
    * @Description: 新增或修改
    * @Author: YQN
    * @Version: 1.0
    * @Date:  2022/6/24 10:04
    * @Parameters: [dicDetail]
    * @Return cn.book.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody DicDetail dicDetail) {
        try {
            if (dicDetail.getId() != null) {
                dicDetailService.update(dicDetail);
            } else {
                dicDetailService.save(dicDetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }

    }

    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.She
     * @Version: 1.0
     * @Date:  2022/6/25 9:47
     * @Parameters: [ids]
     * @Return cn.itsource.basic.utils.AjaxResult
     */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            dicDetailService.batchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }


}
